<template>
  <div v-if="showdiv">
    <gz-modal
      v-model="showModal"
      type="custom"
      :title="'产品路由'"
      @confirm="confirmFn"
      @cancel="cancelFn"
      @changeShow="cancelFn"
    >
      <template slot="content">
        <div class="product-poup-box">
          <div
            class="product-poup-li"
            v-for="(item, index) in routePropByProdList"
            :key="item.a"
          >
            <label class="lable">{{ item.propName }}</label>
            <div class="pro-zhi">
              <gz-route-prop
                v-model="item.selValue"
                :datalist="[item.dictList]"
              ></gz-route-prop>
            </div>
          </div>
        </div>
      </template>
    </gz-modal>
  </div>
</template>

<script>
/**
 * <pro-route v-if="show" :show="show" :proId="proId" :rdsuserinfoData="rdsuserinfo" @handleChange="handleChange"></pro-route>
 */
import Vue from "vue";
import { Icon } from "vant";
import { gzModal, gzRouteProp } from "@/components/customize";
export default {
  components: {
    gzModal,
    [Icon.name]: Icon,
    gzRouteProp,
  },
  props: ["show", "proId", "proCode", "rdsuserinfoData", "appId"],
  data: function () {
    return {
      iswxXcx: "", //设备信息
      showdiv: this.show,
      showModal: false,
      linkDataId: this.proId,
      rdsuserinfo: this.rdsuserinfoData || {},
      routePropByProdList: [], //产品路由数据
    };
  },
  created: function () {
    this.iswxXcx = process.env.Equipment;
    this.queryTask(this.appId,this.proCode);
  },
  methods: {
    // 获取可用产品
    confirmFn() {
      var Query = [];
      var isPost = true;
      this.routePropByProdList.forEach((item) => {
        if (!item.selValue || item.selValue.length < 1) {
          this.$vux.toast.show({
            text: "请设置" + item.propName,
            type: "warn",
          });
          isPost = false;
        }
        var obj = {
          propertyId: item.propId,
          proDataDictValIds: item.selValue,
        };
        Query.push(obj);
      });
      if (!isPost) {
        return;
      }
      let url = this.__ctx + "/pro/getProdByRoute";
      let post = this.baseService.post(url, {
        type: "MAIN",
        module: "MOBILE",
        productId: this.linkDataId,
        prodPropertyList: Query,
      });
      this.abTools.getResultData(post, (data) => {
        this.loanClick(data, this.linkDataId);
      });
    },
    // 申请
    loanClick(data, produId) {
      this.$vux.loading.show({
        text: "加载中...",
      });
      // 点击贷款按钮调用接口/bpm/instance/doAction
      let defer = this.baseService.post(
        this.__ctx + "/bpm/instance/doActionForCallActivity",
        {
          defId: data.bpmDefId, //产品定义id
          data: {
            incomingBusinessObj: {
              DEVICE_INF: this.iswxXcx, //设备信息
              TWO_DIME_INF: "", //二维码信息
              PRODUCT_INF: data.id, //基础产品信息
              PRODUCT_NUM: produId, //可售产品信息
              WF_LOAN_APPLICATION: {
                PROD_NAM: "",
                LOAN_AMOUNT: "001",
                MAIN_WRNT_TP: "10000",
                PROD_COD: "",
                LOAN_USE: "771"
              },
            },
            suppBusinessObj: {},
            dueDiligenceBusinessObj: {},
            peasantBusinessObj: {},
            reviewAndApprove: {
              BSN_REPLY_REP: "03",
              BSN_REPLY_UNIT: "月",
              BSN_REPLY_LIM: "12",
              MAIN_LON_GRTE_TYP: "1",
              WTHR_RVL: "1",
            },
          },
          action: "start", //默认
          opinion: "启动", //默认
        }
      );
      defer.then((data) => {
        this.$vux.loading.hide();
        if (data.isOk) {
          this.$emit("handleChange", false);
          // 返回值包含taskId，接口发起成功后，默认跳转/bpm/complateTask页面，后拼接taskId
          this.$router.push("/bpm/complateTask?taskId=" + data.data.taskId);
        } else {
          this.$vux.toast.show({
            type: "warn",
            text: data.msg,
          });
        }
      });
    },
    queryTask(appId,code) {
      let url = this.__ctx + "/gz/api/query";
      let post = this.baseService.post(url, {
        appId: appId,
        productId: code,
        deviceInf: process.env.Equipment,
      });
      this.$vux.loading.show({
        text: "加载中...",
      });
      post
        .then((res) => {
          this.$vux.loading.hide();
          if (res.isOk) {
            if (res.data.status && res.data.taskId) {
              this.$router.push({
                path: "/bpm/complateTask",
                query: { taskId: res.data.taskId },
              });
            } else {
              this.getRouteProp(this.linkDataId)
            }
          } else {
            this.$vux.toast.show({
              type: "warn",
              text: res.msg,
            });
          }
        })
        .catch((err) => {
          this.$vux.loading.hide();
        });
    },
    // 获取产品路由数据
    getRouteProp(id) {
      let url = this.__ctx + "/pro/getRoutePropByProdId";
      let post = this.baseService.postForm(url, {
        type: "MAIN",
        module: "MOBILE",
        productId: id, //'438017433219104769'
      });
      this.$vux.loading.show({
        text: "加载中...",
      });
      this.abTools.getResultData(post, (data) => {
        this.$vux.loading.hide();
        if (data.proRouteProdDefVO && data.proRouteProdDefVO.bpmDefId) {
          this.showModal = false;
          this.showdiv = false;
          this.$emit("handleChange", false);
          this.$router.push({
            path: "/bpm/start",
            query: {
              defId: data.proRouteProdDefVO.bpmDefId,
              productInf: data.proRouteProdDefVO.id,
              productNum: id,
            },
          });
          //   this.loanClick(data.proRouteProdDefVO, id);
        } else {
          if (data.routePropValueList.length > 0) {
            let dataList = data.routePropValueList;
            for (var i = 0; i < dataList.length; i++) {
              for (var j = 0; j < dataList[i].dictList.length; j++) {
                dataList[i].dictList[j].value = dataList[i].dictList[j].id;
              }
              if (dataList[i].dictList.length == 1) {
                dataList[i].selValue = [dataList[i].dictList[0].id];
              }
            }
            this.routePropByProdList = dataList;
          }
          this.showModal = true;
        }
      });
    },
    cancelFn() {
      this.showModal = false;
      this.showdiv = false;
      this.$emit("handleChange", false);
    },
  },
};
</script>
<style lang="less" scoped>
.pro-bg-white {
  min-height: 100vh;
  background-color: #ffffff !important;
}
.product-box {
  padding-top: 1em;
  .pro-title {
    height: 22px;
    font-size: 1.4em;
    font-family: PingFang SC-Semibold, PingFang SC;
    font-weight: 600;
    color: #000000;
    line-height: 2em;
    padding: 0 2em 1em;
  }
}
.pro-list {
  display: flex;
  flex-wrap: wrap;
  padding: 0 1em 0.5em;
  .pro {
    width: 33.3%;
    min-width: 33.3%;
    text-align: center;
    margin-top: 0.5em;

    .van-icon {
      font-size: 3em;
      color: #ff4a32;
    }
    div {
      font-size: 1.17em;
      font-family: PingFang SC-Regular, PingFang SC;
      font-weight: 400;
      color: #4e5969;
      margin-top: 1em;
      min-height: 3em;
    }
  }
}
/deep/._v-container {
  margin-top: 50px;
}
/deep/.van-overlay {
  z-index: 8 !important;
}
/deep/.van-popup--bottom.van-popup--round {
  z-index: 9 !important;
}
.product-poup-box {
  padding: 0 2em 1em;
  color: #1d2129;
  .product-poup-li {
    min-height: 4.5em;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid #e5e6eb;
    font-size: 1.3em;
    &:last-child {
      border: 0;
    }
    .lable::after {
      content: "*";
      color: red;
    }
    .pro-zhi {
      flex: 1;
      text-align: right;
      /deep/.select-box {
        background-color: rgba(0, 0, 0, 0);
      }
      /deep/.vux-cell-box:not(:first-child):before {
        border: 0;
      }
    }
    .vux-popup-dialog {
      z-index: 99;
    }
  }
}
</style>
